package pub.wkq.common.utils;

import org.springframework.util.DigestUtils;

/**
 * MD5 加密工具类
 */
public class MD5Utils {

    /**
     * MD5混合加密
     * @param data: 待加密字符串
     * @param slat: 盐，用于混合md5加密
     * @return
     */
    public static String encrypt(String data, String slat) {
        String base = data + slat;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
        return md5;
    }

    public static void main(String[] args) {
       String md5Str = MD5Utils.encrypt("123456", "8111");
        System.out.println(md5Str);
        String randomSalt = getRandomSalt(8);
        System.out.println("randomSalt = " + randomSalt);
    }

    /**
     * 获取随机盐值
     *
     * @param len 盐值长度
     * @return 随机盐值
     */
    public static String getRandomSalt(int len) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char ch = str.charAt((int) (Math.random() * str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

}